/**
 * 
 */
package org.arpit.masteringextjs.dao.impl;

import java.util.List;

import org.arpit.masteringextjs.dao.DBUtility;
import org.arpit.masteringextjs.dao.FilmDAO;
import org.arpit.masteringextjs.domain.City;
import org.arpit.masteringextjs.domain.Film;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 * @author arpit
 *
 */
public class FilmDAOImpl implements FilmDAO{

	private static FilmDAO filmDAO;
	private SessionFactory sessionFactory = DBUtility.getSessionFactory();
	private FilmDAOImpl () {
		
	}
	
	public static FilmDAO getInstance() {
		if(filmDAO == null) {
			filmDAO = new FilmDAOImpl();
		}
		return filmDAO;
	}
	
	@Override
	public List<Film> findAll() {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Criteria crit = session.createCriteria(Film.class);
		crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		List<Film> films = crit.list();
		session.getTransaction().commit();
		session.close();
		return films;
	}

	@Override
	public Film create(Film film) {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Film createdFilm = (Film) session.merge(film);
		session.getTransaction().commit();
		session.close();
		return createdFilm;
	}

	@Override
	public Film update(Film film) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void delete(long id) {
		// TODO Auto-generated method stub
		
	}

}
